package com.face.controller;

import com.alibaba.fastjson.JSON;
import com.face.common.Result;
import com.face.common.ResultCode;
import com.face.common.UserInfo;
import com.face.exception.NoAccountException;
import com.face.form.LoginForm;
import com.face.service.AccountService;
import com.face.util.CookieUtil;
import com.face.util.JwtUtil;
import com.face.wrapper.AccountWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminAuthController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/login")
    public Result login(@RequestBody LoginForm loginForm, HttpServletRequest request, HttpServletResponse response){
        if (StringUtils.isBlank(loginForm.getUsername()) || StringUtils.isBlank(loginForm.getPassword())) {
            return Result.failure(ResultCode.BAD_PARAMETER);
        }
        AccountWrapper account;
        try {
            account = accountService.findByUsernameAndPassword(loginForm.getUsername(), loginForm.getPassword());
            if (account.getStatus() == 1) {
                return Result.failure(ResultCode.UNACTIVE);
            } else if (account.getStatus() == 6) {
                return Result.failure(ResultCode.ACCOUNT_RESTED);
            }
        } catch (NoAccountException e) {
            return Result.failure(ResultCode.USERNAME_OR_PASSWORD_ERROR);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(account.getUsername());
        userInfo.setShopId(account.getShopId());
        userInfo.setStatus(account.getStatus());
        userInfo.setPerms(JSON.parseObject(account.getPermissions(), List.class));
        String token = JwtUtil.createJWT(userInfo, "face", 30 * 60 * 1000);
        CookieUtil.setCookie(request, response, "Token", token, 30 * 60 * 1000, true, true);
        return Result.success();
    }

    @GetMapping("/verify")
    public Result verifyUser(@Nullable @CookieValue("Token")String token, HttpServletRequest request, HttpServletResponse response){
        if (StringUtils.isBlank(token)) {
            return Result.failure(ResultCode.UNANTH);
        }
        try {
            UserInfo userInfo = JwtUtil.parseJWT(token);
            if (userInfo.getStatus() == 4) {
                userInfo.setStatus(0);
                accountService.updateStatusByUsername(userInfo.getUsername(), 0);
                token = JwtUtil.createJWT(userInfo, "face", 30 * 60 * 1000);
                CookieUtil.setCookie(request, response, "Token", token, 30 * 60 * 1000, true, true);
                userInfo.setStatus(4);
            } else {
                token = JwtUtil.createJWT(userInfo, "face", 30 * 60 * 1000);
                CookieUtil.setCookie(request, response, "Token", token, 30 * 60 * 1000, true, true);
            }
            return Result.success(userInfo);
        } catch (Exception e) {
            return Result.failure(ResultCode.UNANTH);
        }

    }

    @PostMapping("/logout")
    public Result logout(HttpServletRequest request, HttpServletResponse response){
        CookieUtil.deleteCookie(request, response, "Token");
        return Result.success();
    }
}
